package cn.ecut.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 获取请求头中的内容
 **/

@WebServlet("/requestMethod02")
public class RequestMethodTest02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**
         * 根据指定的请求头的key ，来获取value
         */
        String secFetchUser = request.getHeader("Sec-Fetch-User");
        System.out.println( secFetchUser ) ; //null

        /**
         * 有时，不清楚请求头中有什么内容，可以使用迭代的方式，进行查看或处理
         */
        Enumeration<String> headerNames = request.getHeaderNames();
        while( headerNames.hasMoreElements() ){
            String name = headerNames.nextElement();
            // 根据 请求头的名字，获取对应的值
            String value = request.getHeader( name ) ;
            System.out.println( name + " : " + value );
            /**
             *
             * Cookie : JSESSIONID=node0tz7ysckub15p114xmtgx4o93x0.node0
             * Accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*//*;
            q = 0.8
                    * Upgrade - Insecure - Requests :1
                    * Connection :keep - alive
                    * User - Agent :Mozilla / 5.0 (Windows NT 10.0;
            WOW64)AppleWebKit / 537.36 (KHTML, like Gecko)Chrome / 69.0 .3497 .100 Safari / 537.36
                    * Host :localhost:
            8080
                    * Accept - Encoding :gzip, deflate, br
                    * Accept - Language :zh - CN, zh;
            q = 0.9
             */
        }
    }
}
